﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using QBZ.Evaluation.Domains;
using QBZ.Evaluation.Domains.OAuth;
using QBZ.Evaluation.Models;
using System;
//[EnableCors("any")]
[Route("api/[controller]/[action]/{id?}")]
[ApiController]
public class OAuthController : Controller
{
    private readonly UserDomain _user;

    public OAuthController( UserDomain user)
    {
        _user = user;
    }

    public ActionResult<string> QQ(string redirect = null)
    {
        var res = new QQ(HttpContext, redirect).Authorize();
        if (res == null)
        {
            return Json(null);
        }
        else
        {
            var userId = 0;
            object userLevel = UserLevel.RegUser;
            //如果已经登陆了其他账户视为绑定
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                int.TryParse(HttpContext.User.FindFirst(_ => _.Type == "UserId").Value, out userId);
                if(!Enum.TryParse(typeof(UserLevel), HttpContext.User.FindFirst(_ => _.Type == "UserLevel").Value, out userLevel))
                {
                    userLevel = UserLevel.NoLogin;
                }
            }
            if (res.status.code == 0)
            {
                var oAuth = _user.FindOAuth((OAuthModel)res.result,userId,(UserLevel)userLevel);
                var token = _user.getToken(new UserBaseModel
                {
                    UserId = oAuth.UserId,
                    UserLevel = oAuth.UserLevel
                });
                /*
                this.HttpContext.Response.StatusCode = 200;
                this.HttpContext.Response.ContentType = "text/html";
                var html = "<html><script>localStorage.token ='"+ token + "';top.location.href=sessionStorage.from;</script><html>";
                this.HttpContext.Response.WriteAsync(html);
                return "";*/
                var res1 = new ResponseModel(ErrReason.Ok);
                res1.result = new { token };
                return Json(res1);
            }
            else
            {
                return res.status.msg;
            }
        }
    }

    public ActionResult<string> Wechat(string redirect = null, bool pc = false)
    {
        var res = new Wechat(HttpContext, redirect, true).Authorize();
        if (res == null)
        {
            return Json(null);
        }
        else
        {
            var userId = 0;
            object userLevel = UserLevel.RegUser;
            //如果已经登陆了其他账户视为绑定
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                int.TryParse(HttpContext.User.FindFirst(_ => _.Type == "UserId").Value, out userId);
                if (!Enum.TryParse(typeof(UserLevel), HttpContext.User.FindFirst(_ => _.Type == "UserLevel").Value, out userLevel))
                {
                    userLevel = UserLevel.NoLogin;
                }
            }
            if (res.status.code == 0)
            {
                var oAuth = _user.FindOAuth((OAuthModel)res.result,userId,(UserLevel)userLevel);
                var token = _user.getToken(new UserBaseModel
                {
                    UserId = oAuth.UserId,
                    UserLevel = oAuth.UserLevel
                });
                /*
                 this.HttpContext.Response.StatusCode = 200;
                this.HttpContext.Response.ContentType = "text/html";
                var html = "<html><script>localStorage.token ='" + token + "';top.location.href=sessionStorage.from;</script><html>";
                this.HttpContext.Response.WriteAsync(html);
               return "";*/
                var res1 = new ResponseModel(ErrReason.Ok);
                res1.result = new { token };
                return Json(res1);
            }
            else
            {
                //return Json(res);
                return res.status.msg;
            }
        }
    }
    /*
    public IActionResult Weibo()
    {
        var res = new Weibo(_contextAccessor).Authorize();

        //if (res != null && res.code == 0)
        return Json(res);

    }
    */


}